package com.jshcbd.modules.asset.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jshcbd.framework.query.QueryGenerator;
import com.jshcbd.framework.vo.Result;
import com.jshcbd.modules.asset.entity.AddressEntity;
import com.jshcbd.modules.asset.entity.CompanyEntity;
import com.jshcbd.modules.asset.service.IAddressService;
import com.jshcbd.modules.asset.service.ICompanyService;
import com.jshcbd.modules.asset.vo.AddressVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhoucd
 * @date
 * @description
 */
@Slf4j
@Api(tags="位置标签管理")
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private IAddressService addressService;


    @ApiOperation("位置列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<AddressVo>> queryPageList(AddressVo vo, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                      @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<AddressVo>> result = new Result();
        QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(vo, req.getParameterMap());
        Page<AddressVo> page = new Page(pageNo, pageSize);
        IPage<AddressVo> pageList = addressService.pageView(page, queryWrapper);
        result.setSuccess(true);
        result.setData(pageList);
        log.info(pageList.toString());
        return result;
    }


    @ApiOperation("获取位置信息")
    @GetMapping( "/getById/{id}")
    public Result<AddressVo> queryPageList(@PathVariable String id,HttpServletRequest req) {
        Result<AddressVo> result = new Result();
        AddressVo entity =   addressService.getViewById(id);
        result.setData(entity);
        return result;
    }

    @ApiOperation("新增地址")
    @PostMapping("/add")
    public Result add(@RequestBody AddressEntity addressEntity) {
        try {

            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("rfid",addressEntity.getRfid());

            if(addressService.count(queryWrapper)> 0){
                throw new Exception("当前标签已绑定位置，不可重复绑定！");
            }

            addressService.save(addressEntity);
            return Result.ok("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }


    @ApiOperation("修改地址信息")
    @PutMapping("/edit")
    public Result edit(@RequestBody AddressEntity addressEntity) {
        try {
            AddressEntity entity = addressService.getById(addressEntity.getId());
            if(entity==null) {
                return Result.error("未找到对应实体");
            }else {
                addressService.updateById(addressEntity);
                return Result.ok("修改成功!");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("操作失败");
        }
    }


    @ApiOperation("获取位置信息更新标签ID")
    @GetMapping( "/getByRFID/{rfid}")
    public Result<AddressVo> getByRFID(@PathVariable String rfid,HttpServletRequest req) {
        Result<AddressVo> result = new Result();
        AddressVo entity =   addressService.getViewByRFID(rfid);
        result.setData(entity);
        return result;
    }



}



